package com.sc.nft.callback;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mate.sdk.Callback;
import com.mate.sdk.domain.NFTNotify;
import com.mate.sdk.domain.Notify;
import com.sc.nft.entity.ChainCallBackLog;
import com.sc.nft.enums.ChainCallBackStatusEnum;
import com.sc.nft.enums.ChainCallBackTypeEnum;
import com.sc.nft.util.WeChatUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 *  消耗NFT
 */
@Component
@RequiredArgsConstructor
@Slf4j
@Scope("prototype")
public class EcologyDestroyCollectionCallBack implements Callback {


    @Override
    public void callback(Notify notify) {
        NFTNotify nftNotify = (NFTNotify) notify;
        if(!notify.isSuccess()){
            WeChatUtil.sendWeChatMessage(StrUtil.format("生态销毁NFT链上处理失败(EcologyDestroyCollectionCallBack),错误信息:{},交易Hash:{}", nftNotify.getMessage(),nftNotify.getTransactionHash()));
        }

        ChainCallBackLog chainCallBackLog = new ChainCallBackLog();
        chainCallBackLog.setCallBackType(ChainCallBackTypeEnum.TRADING_DESTROY);
        chainCallBackLog.setTransactionHash(nftNotify.getTransactionHash());
        chainCallBackLog.setTokenId(nftNotify.getTokenId());
        chainCallBackLog.setTokenIds(nftNotify.getTokenIds());
        chainCallBackLog.setIsSuccess(nftNotify.isSuccess());
        chainCallBackLog.setMessage(nftNotify.getMessage());
        chainCallBackLog.setStatus(ChainCallBackStatusEnum.TO_BE_PROCESSED);
        chainCallBackLog.setResultJson(JSONUtil.toJsonStr(nftNotify));
        chainCallBackLog.insert();
    }
}
